package com.fpj.sat.vehicle_administrator.view.dialog;

import java.awt.Component;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import com.fpj.sat.vehicle_administrator.action.VehiculoAction;
import com.fpj.sat.vehicle_administrator.domain.RegistroVehiculo;
import com.fpj.sat.vehicle_administrator.util.JTextFieldLimit;
import com.fpj.sat.vehicle_administrator.util.RegistroModel;
import com.fpj.sat.vehicle_administrator.util.Util;
import com.fpj.sat.vehicle_administrator.view.PrincipalFrame;
import com.fpj.sat.vehicle_administrator.view.VehiculoPanel;

public class DetalleRegistroDialog extends JDialog{
	
	private static final long serialVersionUID = 2048701096384654016L;
	private VehiculoAction vehiculoAction;
	private JLabel lblTitulo;
	private JLabel lblPlaca;
	private JLabel lblMotor;
	private JLabel lblFecha;
	private JTextField txtPlaca;
	private JTextField txtMotor;
	private UtilDateModel modelFechaVehiculo;
	private JDatePanelImpl datePanelFechaVehiculo; 
	private JDatePickerImpl dtFechaVehiculo;
	private JScrollPane spTable;
	private JTable tblVehiculos;
	private RegistroModel modelVehiculos;
	private JButton btnGuardar;
	private JButton btnVerPdf;
	private JButton btnEnviarCorreo;
	
	private final String[] columnasRegistro = {"ID", "PLACA", "MOTOR", "FECHA VEHICULO", "ID REGISTRO"};
	
	public DetalleRegistroDialog(VehiculoPanel vehiculoPanel, PrincipalFrame principal, RegistroVehiculo registroVehiculo) {
		super(principal);
		this.vehiculoAction = new VehiculoAction(this, vehiculoPanel, principal, registroVehiculo);
		inicializaDialog();
		inicializaComponentes();
		inicializaData();
	}

	private void inicializaDialog() {
		setLayout(null);
		setBounds(Util.getAnchoPantalla(getToolkit().getScreenSize(), 0.1), 0, Util.getAnchoPantalla(getToolkit().getScreenSize(), 0.8), Util.getAltoPantalla(getToolkit().getScreenSize(), 1));
	}

	private void inicializaComponentes() {
		lblTitulo = new JLabel("DETALLE DE REGISTRO VEHICULAR");
		lblTitulo.setFont(new Font(lblTitulo.getFont().getFontName(), Font.BOLD, 16));
		lblTitulo.setBounds(Util.getPorcentaje(getWidth(), 0.5) - 140, 40, 320, 25);
		
		lblPlaca = new JLabel("PLACA:");
		lblPlaca.setBounds(20, 120, 120, 20);
		
		lblMotor = new JLabel("MOTOR:");
		lblMotor.setBounds(220, 120, 120, 20);
		
		lblFecha = new JLabel("FECHA:");
		lblFecha.setBounds(420, 120, 120, 20);
		
		txtPlaca = new JTextField();
		txtPlaca.setDocument(new JTextFieldLimit(7));
		txtPlaca.setBounds(80, 120, 120, 25);
		txtPlaca.addKeyListener(vehiculoAction);
		
		txtMotor = new JTextField();
		txtMotor.setDocument(new JTextFieldLimit(15));
		txtMotor.setBounds(280, 120, 120, 25);
		txtMotor.addKeyListener(vehiculoAction);
		
		modelFechaVehiculo = new UtilDateModel();
		datePanelFechaVehiculo = new JDatePanelImpl(modelFechaVehiculo);
		dtFechaVehiculo = new JDatePickerImpl(datePanelFechaVehiculo);
		dtFechaVehiculo.setBounds(480, 115, 120, 30);
		
		modelVehiculos = new RegistroModel(columnasRegistro, 0);
		tblVehiculos = new JTable(modelVehiculos);
		tblVehiculos.setFillsViewportHeight(true);
		tblVehiculos.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tblVehiculos.getSelectionModel().addListSelectionListener(this.vehiculoAction);
		setearColumnsWidth();
		spTable = new JScrollPane(tblVehiculos);
		spTable.setBounds(20, Util.getPorcentaje(getHeight(), 0.22), Util.getPorcentaje(getWidth(), 0.92), Util.getPorcentaje(getHeight(), 0.55));
		
		btnGuardar = new JButton("Guardar");
		btnGuardar.setBounds(620, 120, 100, 25);
		btnGuardar.addActionListener(this.vehiculoAction);
		
		btnVerPdf = new JButton("Ver Documento");
		btnVerPdf.setBounds(20, Util.getPorcentaje(getHeight(), 0.8), 140, 25);
		btnVerPdf.addActionListener(this.vehiculoAction);
		
		btnEnviarCorreo = new JButton("Enviar Correo");
		btnEnviarCorreo.setBounds(180, Util.getPorcentaje(getHeight(), 0.8), 140, 25);
		btnEnviarCorreo.addActionListener(this.vehiculoAction);
		
		agregarComponentes(lblTitulo, lblPlaca, lblMotor, lblFecha, txtPlaca, txtMotor, dtFechaVehiculo, spTable, btnGuardar, btnVerPdf, btnEnviarCorreo);
	}

	private void setearColumnsWidth() {
		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    	renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
		for (int i = 0; i < columnasRegistro.length; i++) {
		    if (i == 0) {
		    	tblVehiculos.getColumn(columnasRegistro[i]).setCellRenderer( renderer);
		    	tblVehiculos.getColumn(columnasRegistro[i]).setMaxWidth(40);
		    }else{
		    	tblVehiculos.getColumn(columnasRegistro[i]).setCellRenderer( renderer);
		    	tblVehiculos.getColumn(columnasRegistro[i]).setMaxWidth(220);
		    }
		}
	}
	
	private void inicializaData() {
		this.vehiculoAction.buscarVehiculos();
	}
	
	private void agregarComponentes(Component...componentes){
		for(Component component: componentes){
			add(component);
		}
		
	}
	
	@Override
	public void setVisible(boolean value) {
	    super.setVisible(value);
	    if(value)
	    	txtMotor.requestFocusInWindow();
	}

	public JTextField getTxtPlaca() {
		return txtPlaca;
	}

	public void setTxtPlaca(JTextField txtPlaca) {
		this.txtPlaca = txtPlaca;
	}

	public JTextField getTxtMotor() {
		return txtMotor;
	}

	public void setTxtMotor(JTextField txtMotor) {
		this.txtMotor = txtMotor;
	}

	public UtilDateModel getModelFechaVehiculo() {
		return modelFechaVehiculo;
	}

	public void setModelFechaVehiculo(UtilDateModel modelFechaVehiculo) {
		this.modelFechaVehiculo = modelFechaVehiculo;
	}

	public JDatePanelImpl getDatePanelFechaVehiculo() {
		return datePanelFechaVehiculo;
	}

	public void setDatePanelFechaVehiculo(JDatePanelImpl datePanelFechaVehiculo) {
		this.datePanelFechaVehiculo = datePanelFechaVehiculo;
	}

	public JDatePickerImpl getDtFechaVehiculo() {
		return dtFechaVehiculo;
	}

	public void setDtFechaVehiculo(JDatePickerImpl dtFechaVehiculo) {
		this.dtFechaVehiculo = dtFechaVehiculo;
	}

	public JTable getTblVehiculos() {
		return tblVehiculos;
	}

	public void setTblVehiculos(JTable tblVehiculos) {
		this.tblVehiculos = tblVehiculos;
	}

	public RegistroModel getModelVehiculos() {
		return modelVehiculos;
	}

	public void setModelVehiculos(RegistroModel modelVehiculos) {
		this.modelVehiculos = modelVehiculos;
	}

	public String[] getColumnasRegistro() {
		return columnasRegistro;
	}

	public JButton getBtnGuardar() {
		return btnGuardar;
	}

	public void setBtnGuardar(JButton btnGuardar) {
		this.btnGuardar = btnGuardar;
	}

	public JButton getBtnVerPdf() {
		return btnVerPdf;
	}

	public void setBtnVerPdf(JButton btnVerPdf) {
		this.btnVerPdf = btnVerPdf;
	}

	public JButton getBtnEnviarCorreo() {
		return btnEnviarCorreo;
	}

	public void setBtnEnviarCorreo(JButton btnEnviarCorreo) {
		this.btnEnviarCorreo = btnEnviarCorreo;
	}
	
	
}
